const {ccclass, property} = cc._decorator;
import JoyStick from "./JoyStick";
import AppDelegate from "./AppDelegate";
import { dispatchEvent } from "./Managers/EventManager";
import { EVT_CREATE_PLAYER } from "./Core/AppEvent";
import { PLAYER_TYPE, WEAPON_TYPE } from "./Core/AppDef";
import WindowUtils from "./Tools/WindowUtils";


@ccclass
export default class MainLayer extends cc.Component {
    @property(cc.Node)
    joyStick: cc.Node = null;
    @property(cc.Node)
    weaponJoyStick: cc.Node = null;

    tempJoyStick = null;

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);

        this.joyStick.active = false;
        this.weaponJoyStick.active = false;
        
        dispatchEvent(EVT_CREATE_PLAYER, {playerType:PLAYER_TYPE.My, weapontype:WEAPON_TYPE.My,
            posx:WindowUtils.DEVICE_W/2 - 200, posy:WindowUtils.DEVICE_H/2})
            
        dispatchEvent(EVT_CREATE_PLAYER, {playerType:PLAYER_TYPE.Enumy, weapontype:WEAPON_TYPE.Enumy,
            posx:1000, posy:550})
    }

    private onTouchStart(event) {
        if(event.getLocation().x < 640){
            this.tempJoyStick = this.joyStick;
        }else{
            this.tempJoyStick = this.weaponJoyStick;
        }
        this.tempJoyStick.active = true;
        this.tempJoyStick.getComponent(JoyStick).onTouchStart(event);
    }

    private onTouchMove(event) {
        this.tempJoyStick.active = true;
        this.tempJoyStick.getComponent(JoyStick).onTouchMove(event);
    }

    private onTouchEnd(event) {
        this.tempJoyStick.active = false;
        this.tempJoyStick.getComponent(JoyStick).onTouchEnd(event);
    }

}
